<?php require_once 'httpFilter.php'; 'conf.php'; ?>
<html>
<title>Weekly Reconciliation</title>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
<link rel="stylesheet" type="text/css" media="screen"
	href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/ui-lightness/jquery-ui.css" />


<script type="text/javascript"
	src="http://www.google.com/jsapi?key=ABQIAAAAN5ZtdO4QwxMtTu20xDDGaBRDmw7J9fBy0SHOM2DFag_Hy1S5lhQ5rqB1kcjLa1mWGmKBHM2W7xPDyQ">
</script>
<script>
	google.load("jquery", "1.4.2");
	google.load("jqueryui", "1.7.2");
</script>

<script type="text/javascript" src="weeklyReconciliation.js">
</script>
<script type="text/javascript" src="js/utils.js"></script>
</head>
<body>
<div id="msgDlg"></div>
<div id="confirmDlg"></div>
<input type="hidden" id="budgetId" value="<?=$_GET["budgetId"]?>" />

<?php include("includes/navigation.php"); ?>

<table id="mytable" cellspacing="0" summary="August 2010 Budget">
	<caption>"Getting Rich Slowly"</caption>
	<?php
	$userId = $_SESSION["userid"];
	$budgetId = $_GET["budgetId"];
	if ($budgetId == "current"){
		$budgetId = $commonServicesInst->getCurrentBudgetId($userId);
	}
	
	$startDate = $commonServicesInst->getStartDateForBudget($budgetId);
	$endDate = $commonServicesInst->getEndDateForBudget($budgetId);

	$con = mysql_connect(GlobalConfig::db_host_name,GlobalConfig::db_username,GlobalConfig::db_password);
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db(GlobalConfig::db_name, $con);

	$result = mysql_query("select * from budget where id=" . $budgetId);
	while($row = mysql_fetch_array($result))
	{
		$budgetName = $row['month'];
	}
	?>
	<tr>
		<th scope="row" colspan="4" class="spec"><?php echo $budgetName ?></th>
	</tr>
	<tr>
		<th scope="col">Expense</th>
		<th scope="col" width="100">Budget</th>
		<th scope="col" width="100">Actual</th>
		<th scope="col" width="100">Remaining</th>
	</tr>
	<?php
	mysql_select_db(GlobalConfig::db_name, $con);
	$result = mysql_query("select b.budgetAmount, b.actualAmount, i.id, i.description FROM budget_budgetitem b, budgetitem i where b.budgetid=". $budgetId ." and i.id=b.budgetitemid and categoryId=2");

	$budgetTotal = 0;
	$actualTotal = 0;

	while($row = mysql_fetch_array($result))
	{
		$result1 = mysql_query("select sum(amount) as sum from transaction where budgetitemid=". $row['id'] ." and date(date) >= '". $startDate ."' and date(date) < '". $endDate ."'");
		error_log($result1);
		$sum = mysql_fetch_array($result1);

		$budgetAmount = $row['budgetAmount'];
		$actualAmount = $row['actualAmount'];

		$budgetTotal += $budgetAmount;
		$actualTotal -= $sum["sum"];


	 ?>
	<tr id="<?= $row['id']?>">
		<th scope="row" class="spec"><?php echo $row['description'] ?></th>
		<td class="spec">R <label name="budget" class="weeklyBudget"><?php echo $budgetAmount ?></label></td>
		<td class="spec">R <label name="actual"><?php echo $sum["sum"] * -1 ?></label></td>
		<td class="spec">R <label name="variableDifferenceAmt"><?php echo ($budgetAmount - $actualAmount) ?></label></td>
	</tr>
	<?php
	}
	$totalRemaining = ($budgetTotal - $actualTotal);
	?>

	<tr>
		<th scope="row" class="nobg">&nbsp;</th>
		<td class="total">R <?=$budgetTotal ?></td>
		<td class="total">R <?=$actualTotal ?></td>
		<td class="total">R <?=$totalRemaining ?></td>
	</tr>
</table>
<br />
<table cellspacing="0" cellpadding="0" width="900">
	<tr>
		<th scope="col">Expense</th>
		<th scope="col" width="90">Spend/week</th>
		<th scope="col" colspan="1"width="100">Week1</th>
		<th scope="col" colspan="1" width="100">Week2</th>
		<th scope="col" colspan="1" width="100">Week3</th>
		<th scope="col" colspan="1" width="100">Week4</th>
		<th scope="col" colspan="1" width="100">Week5</th>
	</tr>
	
	<?php
	$week1StartDate =  $commonServicesInst->getStartOfWeek($budgetId, 1);
	$week1EndDate =  $commonServicesInst->getEndOfWeek($budgetId, 1);	
	$week2StartDate =  $commonServicesInst->getStartOfWeek($budgetId, 2);	
	$week2EndDate =  $commonServicesInst->getEndOfWeek($budgetId, 2);
	$week3StartDate =  $commonServicesInst->getStartOfWeek($budgetId, 3);
	$week3EndDate =  $commonServicesInst->getEndOfWeek($budgetId, 3);
	$week4StartDate =  $commonServicesInst->getStartOfWeek($budgetId, 4);
	$week4EndDate =  $commonServicesInst->getEndOfWeek($budgetId, 4);
	$week5StartDate =  $commonServicesInst->getStartOfWeek($budgetId, 5);
	$week5EndDate =  $commonServicesInst->getEndOfWeek($budgetId, 5);
	
		
	
	$con = mysql_connect(GlobalConfig::db_host_name,GlobalConfig::db_username,GlobalConfig::db_password);
	mysql_select_db(GlobalConfig::db_name, $con);
	$result = mysql_query("SELECT b.budgetAmount, b.actualAmount, i.id, i.description FROM budget_budgetitem b, budgetitem i where b.budgetid=". $budgetId ." and i.id=b.budgetitemid and categoryId=2");

	$budgetTotal = 0;
	$actualTotal = 0;
	error_log("got start");
	$array_result = array();
	while($arow = mysql_fetch_array($result))
	{
		array_push($array_result,$arow);
	}
	foreach ($array_result as $row)
	{
		$week1 = mysql_query("select SUM(amount) as sum from transaction where budgetitemid=". $row['id'] ." and date(date) >= '". $week1StartDate ."' and date(date) < '". $week1EndDate ."'");
		$week1Sum = mysql_fetch_array($week1);
		
		$week2 = mysql_query("select SUM(amount) as sum from transaction where budgetitemid=". $row['id'] ." and date(date) >= '". $week2StartDate ."' and date(date) < '". $week2EndDate ."'");
		$week2Sum = mysql_fetch_array($week2);
		
		$week3 = mysql_query("select SUM(amount) as sum from transaction where budgetitemid=". $row['id'] ." and date(date) >= '". $week3StartDate ."' and date(date) < '". $week3EndDate ."'");
		$week3Sum = mysql_fetch_array($week3);
		
		$week4 = mysql_query("select SUM(amount) as sum from transaction where budgetitemid=". $row['id'] ." and date(date) >= '". $week4StartDate ."' and date(date) < '". $week4EndDate ."'");
		$week4Sum = mysql_fetch_array($week4);
		
		$week5 = mysql_query("select SUM(amount) as sum from transaction where budgetitemid=". $row['id'] ." and date(date) >= '". $week5StartDate ."' and date(date) < '". $week5EndDate ."'");
		$week5Sum = mysql_fetch_array($week5);
	?>
	<tr>
		<th scope="row" class="spec" colspan="1"> <?=$row["description"] ?></th>
		<td class="spec">R <label class="weeklySpend"></label></td>
		<td class="spec">R <label class="week1"><?=$week1Sum["sum"] * -1 ?></label></td>
		<td class="spec">R <label class="week2"><?=$week2Sum["sum"] * -1 ?></label></td>
		<td class="spec">R <label class="week3"><?=$week3Sum["sum"] * -1 ?></label></td>
		<td class="spec">R <label class="week4"><?=$week4Sum["sum"] * -1 ?></label></td>
		<td class="spec">R <label class="week5"><?=$week5Sum["sum"] * -1 ?></label></td>
	</tr>
	
	<? 
	}
	?>
	<tr>
		<th scope="row" class="spec" colspan="1">&nbsp;</th>
		<td class="total" id="weeklySpendTotal"></td>
		<td class="total" id="weekly1Total"></td>
		<td class="total" id="weekly2Total"></td>
		<td class="total" id="weekly3Total"></td>
		<td class="total" id="weekly4Total"></td>
		<td class="total" id="weekly5Total"></td>
	</tr>
</table>

<?php
	mysql_close($con);
?>

<?php include("includes/footer.php"); ?>
</body>
</html>
